package com.lwc.backendsinglestructure.service.impl;

import com.lwc.backendsinglestructure.constants.LogConstant;
import com.lwc.backendsinglestructure.service.TestService;
import com.lwc.backendsinglestructure.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class TestServiceImpl implements TestService {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public String test() {
        log.info("开始测试,同步处理,链路ID：{}", MDC.get(LogConstant.TRACE_ID));
//        log.info("测试成功,同步处理,链路ID：{}", ThreadLocalUtil.get().get(LogConstant.TRACE_ID));
        TestService bean = applicationContext.getBean(TestService.class);
        bean.asyncTest();
        return "你好";
    }

    @Async("CommonThreadPoolExecutor")
    public void asyncTest() {
        try {
            Thread.sleep(2000);
            log.info("测试成功,异步处理,链路ID：{}", MDC.get(LogConstant.TRACE_ID));
//            log.info("测试成功,异步处理,链路ID：{}", ThreadLocalUtil.get().get(LogConstant.TRACE_ID));
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
